package com.cskaoyan.shopping.service.servicebyq.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.PanelConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.converter.ProductDetailConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;

import com.cskaoyan.shopping.service.IProductService;
import com.cskaoyan.shopping.service.servicebyq.ShoppingServiceByQ;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Qing
 * @since 2022/06/17 16:36
 */
@Service
public class ShoppingServiceImplByQ implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductDetailConverter productDetailConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelConverter panelConverter;

    @Autowired
    ProductConverter productConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {

        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        try {
            // 参数校验
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productDetailConverter.item2ProductDto(item);

            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());

            productDetailDto.setDetail(itemDesc.getItemDesc());

            // 成功执行
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            productDetailResponse.setProductDetailDto(productDetailDto);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse, e);
        }
        return productDetailResponse;

    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try {
            // 参数校验，确保page和size不为空
            request.requestCheck();

            //开始逻辑
            Integer page = request.getPage();
            Integer size = request.getSize();
            String sort = request.getSort();
            Integer priceGt = request.getPriceGt();
            Integer priceLte = request.getPriceLte();
            Long cid = request.getCid();

            // 开始分页插件
            PageHelper.startPage(page, size);

            List<Item> items = null;

            // 若所有附加条件都是空，那就把所有的都查出来
            if (cid == null && (sort == null || sort.equals("")) && priceGt == null && priceLte == null){
                items = itemMapper.selectAll();
            }else {
                // 有条件不是空，那么就按照条件往example里放值
                Example example = new Example(Item.class);
                Example.Criteria criteria = example.createCriteria();
                // 首次按判断cid 若cid == null，就查询全部，若不等于null，就查询某个类别的
                if (cid != null){
                    criteria.andEqualTo("cid",cid);
                }
                // 其次判断 sort， Sort = 1 默认值，Sort = -1 desc
                if (sort != null){
                    if (sort.equals("1")){
                        example.setOrderByClause("price");
                    }else if (sort.equals("-1")){
                        example.setOrderByClause("price desc");
                    }
                }
                // 判断最高价
                if (priceLte != null){
                    criteria.andLessThanOrEqualTo("price",priceLte);
                }
                // 判断最低价
                if (priceGt != null){
                    criteria.andGreaterThanOrEqualTo("price",priceGt);
                }

                // 将所有可能性都装入example之后，使用example查询
                items = itemMapper.selectByExample(example);
            }

            // item 转换 为productDto
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            // 创建pageInfo对象，获取total
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();



            // 装入response
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDtoList(productDtos);
            response.setTotal(total);

        }catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }



    @Override
    public RecommendResponse getRecommendGoods() {


        RecommendResponse recommendResponse = new RecommendResponse();
        try {


            List<PanelDto> panels = panelMapper.selectRecommend();
            for (PanelDto panel : panels) {

                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                List<PanelContentItemDto> panelContentItemDtos = panelConverter.panelContentItems2Dto(panelContentItems);
                panel.setPanelContentItems(panelContentItemDtos);
            }


            // 成功执行
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            Set<PanelDto> panelDtoSet = new  HashSet<>(panels);
            recommendResponse.setPanelContentItemDtos(panelDtoSet);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }
        return recommendResponse;


    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}